﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using NthParameterSeries.Models;
using System.Windows.Data;

namespace NthParameterSeries.Views
{
    public partial class PortfolioMaintenanceView : Page
    {
        public PortfolioMaintenanceView()
        {
            InitializeComponent();
            DataContext = new PortfolioMaintenanceViewModel();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {

            // Do not load your data at design time.
            // if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            // {
            // 	//Load your data here and assign the result to the CollectionViewSource.
            // 	System.Windows.Data.CollectionViewSource myCollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["Resource Key for CollectionViewSource"];
            // 	myCollectionViewSource.Source = your data
            // }
            // Do not load your data at design time.
            // if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            // {
            // 	//Load your data here and assign the result to the CollectionViewSource.
            // 	System.Windows.Data.CollectionViewSource myCollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["Resource Key for CollectionViewSource"];
            // 	myCollectionViewSource.Source = your data
            // }
            // Do not load your data at design time.
            // if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            // {
            // 	//Load your data here and assign the result to the CollectionViewSource.
            // 	System.Windows.Data.CollectionViewSource myCollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["Resource Key for CollectionViewSource"];
            // 	myCollectionViewSource.Source = your data
            // }
        }

        private void daoPortfolioListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var viewModel = DataContext as PortfolioMaintenanceViewModel;
            if (e.AddedItems.Count > 0)
                viewModel.CurrentPortfolioItem = e.AddedItems.OfType<NthParameterSeries.PriceServiceReference.Portfolio>().First<NthParameterSeries.PriceServiceReference.Portfolio>();
            

        }

        private void AddButton_Click(object sender, RoutedEventArgs e)
        {
            var viewModel = DataContext as PortfolioMaintenanceViewModel;
            viewModel.AddNewItem();
        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            var viewModel = DataContext as PortfolioMaintenanceViewModel;
            viewModel.Save();
        }

        private void countries_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var viewModel = DataContext as PortfolioMaintenanceViewModel;
            if (e.AddedItems.Count > 0 && viewModel.CurrentPortfolioItem != null)
                viewModel.CountryId = e.AddedItems.OfType<NthParameterSeries.PriceServiceReference.Country>().First<NthParameterSeries.PriceServiceReference.Country>().Id;
        }

        private void nameComboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var viewModel = DataContext as PortfolioMaintenanceViewModel;
            if (e.AddedItems.Count > 0 && viewModel.CurrentPortfolioItem != null)
                viewModel.CurrentPortfolioItem.CityId = e.AddedItems.OfType<NthParameterSeries.PriceServiceReference.City>().First<NthParameterSeries.PriceServiceReference.City>().Id;

        }
    }
}
